<?php
class PassCommand extends CConsoleCommand {

	private $return;
	private $data;
	private $sw;

	public function beforeAction(){
		return true;
	}

	public function actionIndex($g=false, $a=false, $s=false, $del=false, $k=false, $p=false, $r=false) {

		$host_api = Yii::app()->params['site_api'];
		$sk = Yii::app()->params['secret_key'];
		$this->sw = Yii::app()->params['secret_word'];

		if($g != "*") $g = $this->encode($g);
		$a 	 = $this->encode($a);
		$s 	 = $this->encode($s);
		$del = $this->encode($del);
		$k   = $this->encode($k);
		$p   = $this->encode($p, true);
		$r   = $this->encode($r);

		if(isset($_SERVER['toKey'])){
			$sk = $sk.$_SERVER['toKey'];
		}
		if($g){
			$this->return = Yii::app()->curl
				->post($host_api.'/api/get',array('k1'=>$g, 'sk'=>$sk));
		}elseif($a){
			$this->return = Yii::app()->curl
				->post($host_api.'/api/add',array('k1'=>$a, 'k2'=>$k, 'p'=>$p, 'sk'=>$sk));
		}elseif($s){
			$this->return = Yii::app()->curl
				->post($host_api.'/api/set',array('k1'=>$s, 'k2'=>$k, 'p'=>$p, 'sk'=>$sk));
		}elseif($del){
			$this->return = Yii::app()->curl
				->post($host_api.'/api/del',array('k1'=>$del, 'k2'=>$k, 'sk'=>$sk));
		}else{
			echo "Not command\n";
		}
	}

	public function afterAction(){
		$this->data = CJSON::decode($this->return);

		if(isset($this->data['data']) and is_array($this->data['data'])){
			foreach ($this->data['data'] as $v) {

				echo 
					"\033[32m".$this->decode($v['key1'])."\033[m ".
					"\033[31m➤\033[m ".
					"\033[33m".$this->decode($v['key2'])."\033[m ".
					"\033[31m➤\033[m ".
					"\033[36m".$this->decode($v['param'], true)."\033[m\n"
				;
			}
		}

		if(isset($this->data['msg'])){
			if(isset($this->data['status']) and $this->data['status'] == 'error'){
				echo "\033[31mError: ".$this->data['msg']."\033[m\n";
			}elseif(isset($this->data['status']) and $this->data['status']=='warning'){
				echo "\033[33mWarning: ".$this->data['msg']."\033[m\n";
			}else{
				echo $this->data['msg']."\n";
			}
		}
		exit();
	}


	private function encode($data="", $important=false){
		if($data){
			if($important)
				return bin2hex(Yii::app()->securityManager->encrypt($data));	
			else
				return $this->fnEncrypt($data, $this->sw);
		}else{
			return "";
		}
	}

	private function decode($data="", $important=false){
		if($data){
			if($important)
				return (Yii::app()->securityManager->decrypt(hex2bin($data)));	
			else
				return $this->fnDecrypt($data, $this->sw);
		}else{
			return "";
		}
	}

	public function fnEncrypt($sValue, $sSecretKey){
	    return rtrim(
	        base64_encode(
	            mcrypt_encrypt(
	                MCRYPT_RIJNDAEL_256,
	                $sSecretKey, $sValue, 
	                MCRYPT_MODE_ECB, 
	                mcrypt_create_iv(
	                    mcrypt_get_iv_size(
	                        MCRYPT_RIJNDAEL_256, 
	                        MCRYPT_MODE_ECB
	                    ), 
	                    MCRYPT_RAND)
	                )
	            ), "\0"
	        );
	}

	public function fnDecrypt($sValue, $sSecretKey){
	    return rtrim(
	        mcrypt_decrypt(
	            MCRYPT_RIJNDAEL_256, 
	            $sSecretKey, 
	            base64_decode($sValue), 
	            MCRYPT_MODE_ECB,
	            mcrypt_create_iv(
	                mcrypt_get_iv_size(
	                    MCRYPT_RIJNDAEL_256,
	                    MCRYPT_MODE_ECB
	                ), 
	                MCRYPT_RAND
	            )
	        ), "\0"
	    );
	}
}